package com.mecol.library.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mecol.library.dao.BookDao;
import com.mecol.library.entity.Book;
import com.mecol.library.entity.BookSearch;
import com.mecol.library.service.BookService;
import com.mecol.library.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookServiceImpl implements BookService{

    @Autowired
    private BookDao bookDao;


    @Override
    public ResultUtil getAllBookList(Integer page, Integer limit, BookSearch search) {
        PageHelper.startPage(page,limit);
        List<Book> books=bookDao.getAllBookList(search);
        PageInfo<Book> pageInfo=new PageInfo<>(books);
        ResultUtil resultUtil=new ResultUtil();
        resultUtil.setCode(0);
        resultUtil.setCount(pageInfo.getTotal());
        resultUtil.setData(pageInfo.getList());
        return resultUtil;
    }

    @Override
    public void addNewBook(Book book) {
        bookDao.insertBook(book);
    }

    @Override
    public Book getBookById(int book_id) {
        return bookDao.getBookById(book_id);
    }

    @Override
    public ResultUtil updateBook(Book book) {
         bookDao.updateBook(book);
         return ResultUtil.ok();
    }

    @Override
    public ResultUtil deleteBookById(int book_id) {
         bookDao.deleteBookById(book_id);
         return ResultUtil.ok();
    }

    @Override
    public List<Book> getAllBooks() {

        return bookDao.getAllBooks();
    }
}
